package com.ag.hmk.action;

import javax.annotation.Resource;

import com.ag.bean.TbCasekw;
import com.ag.bean.TbClassiccase;
import com.ag.bean.TbLevel;
import com.ag.bean.TbType;
import com.ag.common.BaseAction;
import com.ag.common.IDUtil;
import com.ag.common.IIDService;
import com.ag.hmk.service.CaseLevelService;
import com.ag.hmk.service.CaseService;
import com.ag.hmk.service.CasekwService;
import com.ag.hmk.service.HmkUiltService;
import com.ag.hmk.service.WukuTypeService;

/**
 * 
 * @author 黄木看
 * 保存经典案例action
 *
 */
public class SaveCaseAction extends BaseAction {

	private static final long serialVersionUID = 1714149315813458816L;
	private CaseService caseServImpl;
	private WukuTypeService wukuTypeServImpl;
	private CaseLevelService caseLevelServImpl;
	private CasekwService casekwServImpl;
	private HmkUiltService hmkUiltServImpl;
	@Resource(name = "iDServiceImpl")
	private IIDService idservice;
	
	
	private String caseID1;
	private String typeID1;
	private String caseLevel1;
	private String caseName1;
	private String caseDetail1;
	private String caseKwstr1; //案例关键字
	
	private String fileID1;
	private String fileNameID1;
	
	private String ajaxRST;
	
	
	/*
	 * 保存案例
	 */
	public String saveCase() throws Exception{
		this.setAjaxRST("0");
		try {
			TbType tbType = wukuTypeServImpl.findTypeByID(typeID1);
			TbLevel tbLevel = caseLevelServImpl.findByID(caseLevel1);
			if (tbType!=null && tbLevel!=null) {
				if (caseName1!=null && caseName1.length()>0) {
					
					TbClassiccase ccBean = new TbClassiccase();
					ccBean.setCcId(idservice.findTablePK(IDUtil.TB_CLASSICCASE));
					ccBean.setTbType(tbType);
					ccBean.setTbLevel(tbLevel);
					ccBean.setCcName(caseName1);
					ccBean.setCcDetail(caseDetail1);
					ccBean.setCcFjName(fileID1);
					ccBean.setCcFjRote(fileNameID1);
					
					caseServImpl.save(ccBean);  //保存案例
					
					//下面保存关键字
					if (caseKwstr1!=null && caseKwstr1.length()>0) {
						String [] str1 = caseKwstr1.split(",");
						for (int i = 0; i < str1.length; i++) {
							System.out.println("---keyword:"+str1[i]);
							TbCasekw casekw1 = new TbCasekw();
							String ckID = idservice.findTablePK(IDUtil.TB_CASEKW);
							casekw1.setCKwid(ckID);
							casekw1.setCKw(str1[i]);
							casekw1.setTbClassiccase(ccBean);
							
							casekwServImpl.save(casekw1); //保存关键字
							 
							System.out.println("-----保存关键字成功");
						}
						
					} else {
						System.out.println("------没有关键字");
					}
					this.setAjaxRST("1");
					
					System.out.println("--save case ok");
					
				} else {
					System.out.println("---case name and so on can not to be null");
				}
			} else {
				System.out.println("---type can not to be null");
			}
		} catch (Exception e) {
			e.printStackTrace();
			this.setAjaxRST("0");
		}
		
		return "saveCaseOK";
	}

	/*
	 * 更新案例
	 */
	public String updateCase() throws Exception{
		this.setAjaxRST("0");
		try {
			TbClassiccase ccBean = caseServImpl.findByID(caseID1);
			TbType tbType = wukuTypeServImpl.findTypeByID(typeID1);
			TbLevel tbLevel = caseLevelServImpl.findByID(caseLevel1);
			if (tbType!=null && tbLevel!=null) {
				if (ccBean!=null && caseName1!=null && caseName1.length()>0) {
					ccBean.setTbType(tbType);
					ccBean.setTbLevel(tbLevel);
					ccBean.setCcName(caseName1);
					ccBean.setCcDetail(caseDetail1);
					ccBean.setCcFjName(fileID1);
					ccBean.setCcFjRote(fileNameID1);
					
					caseServImpl.save(ccBean);  //更新案例
					
					//删除关键字
					String sql = "DELETE FROM TB_CASEKW WHERE CC_ID="+caseID1;
					hmkUiltServImpl.queryUpdate(sql);
					
					//下面保存关键字
					if (caseKwstr1!=null && caseKwstr1.length()>0) {
						String [] str1 = caseKwstr1.split(",");
						for (int i = 0; i < str1.length; i++) {
							System.out.println("---keyword:"+str1[i]);
							TbCasekw casekw1 = new TbCasekw();
							String ckID = idservice.findTablePK(IDUtil.TB_CASEKW);
							casekw1.setCKwid(ckID);
							casekw1.setCKw(str1[i]);
							casekw1.setTbClassiccase(ccBean);
							
							casekwServImpl.save(casekw1); //保存关键字
							System.out.println("-----保存关键字成功");
						}
						
					} else {
						System.out.println("------没有关键字");
					}
					this.setAjaxRST("1");
					
					System.out.println("--save case ok");
					
				} else {
					System.out.println("---case name and so on can not to be null");
				}
			} else {
				System.out.println("---type can not to be null");
			}
		} catch (Exception e) {
			e.printStackTrace();
			this.setAjaxRST("0");
		}
		
		return "saveCaseOK";
	}
	
	public String getAjaxRST() {
		return ajaxRST;
	}


	public void setAjaxRST(String ajaxRST) {
		this.ajaxRST = ajaxRST;
	}




	public void setTypeID1(String typeID1) {
		this.typeID1 = typeID1;
	}


	public void setCaseLevel1(String caseLevel1) {
		this.caseLevel1 = caseLevel1;
	}


	public void setCaseName1(String caseName1) {
		this.caseName1 = caseName1;
	}


	public void setCaseDetail1(String caseDetail1) {
		this.caseDetail1 = caseDetail1;
	}


	

	


	public void setCaseKwstr1(String caseKwstr1) {
		this.caseKwstr1 = caseKwstr1;
	}


	public void setCaseID1(String caseID1) {
		this.caseID1 = caseID1;
	}

	

	public void setFileID1(String fileID1) {
		this.fileID1 = fileID1;
	}

	public void setFileNameID1(String fileNameID1) {
		this.fileNameID1 = fileNameID1;
	}

	public void setCaseServImpl(CaseService caseServImpl) {
		this.caseServImpl = caseServImpl;
	}

	public void setWukuTypeServImpl(WukuTypeService wukuTypeServImpl) {
		this.wukuTypeServImpl = wukuTypeServImpl;
	}

	public void setCaseLevelServImpl(CaseLevelService caseLevelServImpl) {
		this.caseLevelServImpl = caseLevelServImpl;
	}

	public void setCasekwServImpl(CasekwService casekwServImpl) {
		this.casekwServImpl = casekwServImpl;
	}

	public void setHmkUiltServImpl(HmkUiltService hmkUiltServImpl) {
		this.hmkUiltServImpl = hmkUiltServImpl;
	}





}
